<template>
    <l-table-edit ref="TableEdit" :tableColumns="revenueTableColmuns" :tableData="tableData" @table="tableFunc" />
</template>


<script>
import { revenueTableColmuns, detailApi } from "./columns"
/* 地块历年亩均税收模块 */
export default {
    data() {
        return {
            revenueTableColmuns,
            tableLoading: true,
            detailApi,
            tableData: [],
            tableTotal: 0,
            tableCurrentPage: 1,
            tablePageSize: 50,
        }
    },
    props: {
        landFormData: {
            type: Object,
            default: () => { }
        },
        options: {
            type: Object,
            default: () => { }
        }
    },
    mounted() {
        // this.init();
    },
    methods: {
        handleDelete(index, row) {
            this.$confirm(this.$t('是否删除该条数据?'), this.$t('提示'), {
                confirmButtonText: this.$t('确定'),
                cancelButtonText: this.$t('取消'),
                type: 'warning'
            }).then(async () => {
                await detailApi.remove(row.id)
                this.$message({
                    type: 'success',
                    message: this.$t('删除成功!')
                })
                this.loadTableData()
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: this.$t('已取消删除!')
                })
            })
        },
        init() {
            this.options.isAdd ? this.tableLoading = false : this.loadTableData();
        },
        turnTablePage({ rows }) {
            this.tablePageSize = rows
            this.loadTableData(true)
        },
        async loadTableData(isNotFirst) {
            if (!isNotFirst) {
                this.tableCurrentPage = 1
            }
            this.tableLoading = true;
            let data = await detailApi.get(this.landFormData.id);
            data = data.data;
            if (data.code === 200) {
                this.tableData = data.data.landMuTaxList;
                this.tableData.forEach(i => {
                    // i.isEditing = false 这里要将 isEditing 设置为响应式数据  
                    this.$set(i, "isEditing", false)
                })
                this.tableTotal = data.data.landMuTaxList.length
            }
            else {
                this.tableTotal = 0
                this.tableData = []
            }
            this.tableLoading = false
        },
        tableFunc(res) {
            switch (res.type) {
                case 'save':
                    this.handleSave(res.data)
                    break;
                case 'delete':
                    this.handleDeleteRow(res.data, res.index)
                    break;
                case 'push':
                    this.tableData.push({
                        year: "",
                        coverArea: "",
                        buildingArea: "",
                        plotRatio: "",
                        muTax: "",
                        isEditing: true,
                    })
                    break;
                default:
                    break;
            }
        },
        handleDeleteRow(row, index) {
            this.$confirm(this.$t('是否删除该条数据?'), this.$t('提示'), {
                confirmButtonText: this.$t('确定'),
                cancelButtonText: this.$t('取消'),
                type: 'warning'
            }).then(() => {
                this.tableData.splice(index, 1);
                this.$message({
                    type: 'success',
                    message: this.$t('删除成功!')
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: this.$t('已取消删除!')
                })
            })
        },
        async handleSave(data) {
            // 合法性
            if (!data.year.toString().trim()) {
                this.$message({
                    type: "error",
                    message: "请填入合法字段"
                })
            } else {
                // 无重复性
                let isRepeat = false;
                this.tableData.some(i => {
                    if (data !== i && data.year.toString().trim() === i.year.toString().trim()) {
                        isRepeat = true;
                        return;
                    }
                })
                if (isRepeat) {
                    this.$message({
                        type: "error",
                        message: "年份不能重复"
                    })
                } else {
                    this.$refs.TableEdit.handleCloseEdit(data) // 表格 关闭编辑
                    this.$message({
                        type: "success",
                        message: '保存成功',
                    });
                }
            }
        },
    }
}
</script>


<style scoped lang='scss'></style>